package cn.tedu.mall.order.controller;


import cn.tedu.mall.common.restful.JsonPage;
import cn.tedu.mall.common.restful.JsonResult;
import cn.tedu.mall.order.service.IOmsCartService;
import cn.tedu.mall.order.utils.WebConsts;
import cn.tedu.mall.pojo.order.dto.CartAddDTO;
import cn.tedu.mall.pojo.order.dto.CartUpdateDTO;
import cn.tedu.mall.pojo.order.vo.CartStandardVO;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

@RestController
@RequestMapping("/oms/cart")
@Api(tags = "购物车功能")
public class OmsCartController {

    @Autowired
    private IOmsCartService cartService;

    @PostMapping("/add")
    @ApiOperation("新增sku到购物车")
    //判断当前用户是否为ROLE_user
    @PreAuthorize("hasRole('ROLE_user')")
    //@Validated启动控制器运行前对CartAddDTO对象的SpringValidation验证
    public JsonResult addCart(@Validated CartAddDTO cartAddDTO){
        cartService.addCart(cartAddDTO);
        return JsonResult.ok("添加到购物车完成");
    }

    //根据登录用户id查询购物信息的方法
    @GetMapping("/list")
    @ApiOperation("根据登录用户id查询购物信息的方法")
    @ApiImplicitParams({
            @ApiImplicitParam(value = "页码",name = "page",required = true,dataType = "int"),
            @ApiImplicitParam(value = "每页条数",name = "pageSize",required = true,dataType = "int")
    })
    @PreAuthorize("hasRole('ROLE_user')")

    public JsonResult<JsonPage<CartStandardVO>> listCart(
            @RequestParam(required = false,defaultValue = WebConsts.DEFAULT_PAGE) Integer page,
            @RequestParam(required = false,defaultValue = WebConsts.DEFAULT_PAGE_SIZE) Integer pageSize
    ){
        JsonPage<CartStandardVO> jsonPage = cartService.listCarts(page, pageSize);

        return JsonResult.ok(jsonPage);
    }

    //根据用户指定的sku删除购物车
    @PostMapping("/delete")
    @ApiOperation("根据用户指定的sku删除购物车")
    @ApiImplicitParams({
            @ApiImplicitParam(value = "指定购物车id",name = "ids" ,required = true,dataType = "array")
    })
    @PreAuthorize("hasRole('ROLE_user')")

    public JsonResult removeCarts(Long[] ids){
        cartService.removeCart(ids);
        return JsonResult.ok();
    }

    //根据userId清空购物车

    @PostMapping("/delete/all")
    @ApiOperation("根据userId清空购物车")
    @PreAuthorize("hasRole('ROLE_user')")

    public JsonResult removeCartsByUserId(){
        cartService.removeAllCarts();
        return JsonResult.ok("您的购物车清空完成");
    }


    //更新购物车数量

    @PostMapping("/update/quantity")
    @ApiOperation("更新购物车数量")
    @PreAuthorize("hasRole('ROLE_user')")
    public  JsonResult updateQuantity(@Validated CartUpdateDTO cartUpdateDTO){
        cartService.updateQuantity(cartUpdateDTO);
        return JsonResult.ok("修改完成");
    }






























}
